//
//  CartSalesWithCountDownView.m
//  JiaoX
//
//  Created by jiaoxiu－005 on 15/2/2.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "CartSalesWithCountDownView.h"
#import "TimeUtils.h"
#define SALES_NAME_SIZE [DimensUtils getScaleX:12]

@interface CartSalesWithCountDownView()
{
    UILabel *_salesLabel;
    UILabel *_priceLabel;
    UILabel *_timeLabel;
    NSTimer *_countDownTimer;
}
@end

@implementation CartSalesWithCountDownView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //1.限时抢购价label
        UILabel *salesLabel = [[UILabel alloc ]init];
        NSString *showPriceText = @"限时抢购价:";
        [salesLabel setText:showPriceText];
        [salesLabel setTextColor:JXColor(102, 102, 102)];
        [salesLabel setFont:[UIFont systemFontOfSize:SALES_NAME_SIZE]];
        CGSize showPriceTextFont  = [showPriceText sizeWithFont:[UIFont systemFontOfSize:SALES_NAME_SIZE]];
        [salesLabel setFrame:CGRectMake(0, 0, showPriceTextFont.width, showPriceTextFont.height)];
        _salesLabel = salesLabel;
        [self addSubview:salesLabel];
        //2.抢购价格的label
        UILabel *priceLabel = [[UILabel alloc ]init];
        NSString *priceLabelText = @"￥0";
        [priceLabel setText:priceLabelText];
        [priceLabel setFont:[UIFont systemFontOfSize:SALES_NAME_SIZE]];
        [priceLabel setTextColor:JXThemeColor];
         CGSize priceLabelTextFont  = [showPriceText sizeWithFont:[UIFont systemFontOfSize:SALES_NAME_SIZE]];
        [priceLabel setFrame:CGRectMake(CGRectGetMaxX(_salesLabel.frame), 0, priceLabelTextFont.width, priceLabelTextFont.height)];
        _priceLabel = priceLabel;
        [self addSubview:priceLabel];
        //添加剩余时间
        _timeLabel = [[UILabel alloc]init];
        [_timeLabel setTextColor:JXColor(102, 102, 102)];
        [_timeLabel setFont:[UIFont systemFontOfSize:SALES_NAME_SIZE]];
        [_timeLabel setTextAlignment:NSTextAlignmentRight];
        [_timeLabel setFrame:CGRectMake(CGRectGetMaxX(_salesLabel.frame), 0, JXScreenW-CGRectGetMaxX(_salesLabel.frame)-JXPublicInset*2,[UIFont systemFontOfSize:SALES_NAME_SIZE].lineHeight)];
        [self addSubview:_timeLabel];
    }
    return self;
}

-(void)setPrice:(NSString *)price{//设置抢购价格
    _price = price;
    [_priceLabel setText:[NSString stringWithFormat:@"￥%@",price]];
}

-(void)setTime:(long)time{//设置剩余抢购时间
    NSString *timeStr = [NSString stringWithFormat:@"仅剩 %@",[TimeUtils getFormatRemainTime:[TimeUtils getRemainTime:time]]];
    [_timeLabel setText:timeStr];
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeLabel) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_countDownTimer forMode:NSDefaultRunLoopMode];
}

-(void)timeLabel{
    long reMainTime = [TimeUtils getRemainTime:self.time];
    if(reMainTime<=0){
        [_countDownTimer invalidate];
        //在这写倒计时完了之后需要做的事情
    }else{
        NSString *timeStr = [NSString stringWithFormat:@"仅剩 %@",[TimeUtils getFormatRemainTime:reMainTime]];
        [_timeLabel setText:timeStr];
    }
}
@end
